package com.mingweicai.intergration.thread;

/*
给定一个数组[1,2,3,4,5,6,7,8,9....,15]，要求遍历数组，
遇到可以同时被3和5整除的数字，打印C；遇到仅能被5整除的数字，
打印B；遇到仅能被3整除的数字，打印A；其他打印数字本身
*/
public class FourThread implements Runnable {

  private static final int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

  private int currentIndex;

  public FourThread(int currentIndex) {
    this.currentIndex = currentIndex;
  }

  private int checkFlag(int n) {
    if (n % 15 == 0) {
      return 0;
    } else if (n % 5 == 0) {
      return 1;
    } else if (n % 3 == 0) {
      return 2;
    } else {
      return 3;
    }
  }

  @Override
  public void run() {
    while (true) {
      if(currentIndex >= array.length){
        break;
      }
      int checkFlag = checkFlag(array[currentIndex]);
      switch (checkFlag){
        case 0:
          System.out.println("C");
          break;
        case 1:
          System.out.println("B");
          break;
        case 2:
          System.out.println("A");
          break;
        case 3:
          System.out.println(array[currentIndex]);
          break;
      }
      currentIndex += 4;
    }
  }

  public static void main(String[] args) {
    new Thread(new FourThread(0)).start();
    new Thread(new FourThread(1)).start();
    new Thread(new FourThread(2)).start();
    new Thread(new FourThread(3)).start();
  }

}
